//
//  ScrollWithImage.m
//  UI_转播图
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 YXY. All rights reserved.
//

#import "ScrollWithImage.h"
#import "UIImageView+WebCache.h"
@implementation ScrollWithImage

- (void)dealloc {
    [_timekeeping release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creatScrollView];
    }
    return self;
}

- (void)creatScrollView {
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:scroll];

    scroll.tag = 1000;
    scroll.delegate = self;
    scroll.pagingEnabled = YES;
    
//    _timekeeping = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timer) userInfo:nil repeats:YES];
    
#warning 计时器开始暂停
//    UIButton *timerStop = [UIButton buttonWithType:UIButtonTypeSystem];
//    timerStop.frame = CGRectMake(320, 22, 50, 50);
//    [timerStop setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
//    [timerStop addTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:timerStop];
//    UIButton *timerGo = [UIButton buttonWithType:UIButtonTypeSystem];
//    timerGo.frame = CGRectMake(10, 22, 50, 50);
//    [timerGo setImage:[UIImage imageNamed:@"go"] forState:UIControlStateNormal];
//    [timerGo addTarget:self action:@selector(goButton:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:timerGo];
//    [timerGo release];
//    [timerStop release];
//    [scroll release];
}
- (void)setArray:(NSMutableArray *)array {
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1000];
    if (array.count) {
        [array addObject:[array firstObject]];
    }
    for (int i = 0; i < array.count; i++) {
        if ([array[i] isKindOfClass:[NSString class]]) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, 320)];

        [image sd_setImageWithURL:[NSURL URLWithString:array[i]]];
        [scroll addSubview:image];
        [image release];
        }
    }
    _arrayOne = [[NSMutableArray alloc] init];
    [_arrayOne addObjectsFromArray:array];

    scroll.contentSize = CGSizeMake(scroll.frame.size.width * array.count, 0);
    scroll.showsHorizontalScrollIndicator = NO;
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 90, 250, 80, 30)];
    page.numberOfPages = _arrayOne.count - 1;
    page.pageIndicatorTintColor = [UIColor blackColor];
    page.currentPageIndicatorTintColor = [UIColor redColor];
    page.tag = 1001;
    [page addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    

    [self addSubview:page];
    [page release];
}

- (void)setLabelArray:(NSMutableArray *)labelArray {
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1000];
    for (int i = 0; i < labelArray.count; i++) {
        if ([labelArray[i] isKindOfClass:[NSString class]]) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width * i + 10, 220, self.frame.size.width - 20, 30)];
            label.font = [UIFont systemFontOfSize:15];
            label.text = labelArray[i];
            label.textColor = [UIColor whiteColor];
            [scroll addSubview:label];
            [label release];
        }
    }
}

- (void)pageControlChange:(UIPageControl *)page {
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1000];
    [scroll setContentOffset:CGPointMake(scroll.frame.size.width * page.currentPage, 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UIPageControl *page = (UIPageControl *)[self viewWithTag:1001];
    page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1000];
    if (scroll.contentOffset.x == self.frame.size.width * (_arrayOne.count - 1)) {
            
        [page setCurrentPage:0];
        [scroll setContentOffset:CGPointMake(scroll.frame.size.width * page.currentPage, 0) animated:NO];
    }

    
    
}

#pragma mark - 计时器
- (void)timer {
    UIPageControl *page = (UIPageControl *)[self viewWithTag:1001];

    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1000];
    _times++;
    if (_times >= (_arrayOne.count - 1)) {
        _times = 0;
    }
    [scroll setContentOffset:CGPointMake(_times * self.frame.size.width, 0) animated:YES];
    [page setCurrentPage:_times];

}
-(void)stopButton:(UIButton *)sender {
    [_timekeeping setFireDate:[NSDate distantFuture]];

}
-(void)goButton:(UIButton *)sender {
    [_timekeeping setFireDate:[NSDate distantPast]];
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
